using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class PointTrigger : MonoBehaviour
{
    SpriteRenderer spriteRenderer;
    [SerializeField] Sprite spriteNormal;
    [SerializeField] Sprite spriteEnter;

    bool isEnter;

    public UnityEvent OnClick;

    private void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        OnClick.AddListener(() => { Click(); });
    }

    private void Update()
    {
        MouseExit();
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        bool hitting = Physics.Raycast(ray, out hit, 9999, LayerMask.GetMask("PointTrigger"));
        if (hitting)
        {
            if (hit.collider.gameObject == this.gameObject)
                MouseEnter();
        }
        MouseDown();
    }

    private void MouseEnter()
    {
        isEnter = true;
        spriteRenderer.sprite = spriteEnter;
    }

    private void MouseExit()
    {
        isEnter = false;
        spriteRenderer.sprite = spriteNormal;
    }

    private void MouseDown()
    {
        if (Input.GetMouseButtonDown(0) && isEnter)
        {
            OnClick.Invoke();
        }
    }

    private void Click()
    {
        //Debug.Log("click");
    }
}
